import pymongo

# 这个文件是独立运行的，不用scrapy命令，连接数据库，得到游戏的简要信息
mongo_client = pymongo.MongoClient("mongodb://user_spider:pq888888@192.168.136.128:27017/?authSource=spider")
# print(mongo_client)
mongo_db = mongo_client["spider"]
# print(mongo_db)
collist = mongo_db.list_collection_names()
# print(collist)
coll = mongo_db["MyabandonwareCategorfornameisnull"]
dict_firstSearch={}

str_firstSearch="PLATFORM"
str_secondSearch="THEME"

coll.create_index([("rate",-1)])#创建索引目的是为了减少资源的消耗，要不然下面的语句会崩
for n, i in enumerate(coll.find().sort([('rate', -1)])):#PLATFORM的一次统计
    html_head="""<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <link rel="stylesheet" type="text/css" href="detail.css" />    
    <script type="text/javascript" src="jquery.min.js"></script>
    <script type="text/javascript" src="jquery.lazyload.min.js"></script>   
    <title>%s</title>
</head>
<script>
$(function() {
$("img").lazyload({threshold : 200
});
});
</script>""" %(i["name"])
    html_table="""
<div id="content">
    <div class="box">
        <h2>%s</h2>
    </div>
    <div class="box metas">        <div>            <div class="t409" id="">                <div align="center" >
                </div>            </div>        </div>        <div class="gameData js-gamelist">
            <table class="gameInfo">                <tbody>""" %i["name"]
    if "name" in i:
        html_table=html_table+"""
                        <tr>
                            <th scope="row">%s</th>
                            <td>%s</td>
                        </tr>""" %("name",i["name"])
    if "YEAR" in i:
        html_table=html_table+"""
                        <tr>
                            <th scope="row">%s</th>
                            <td>%s
                            </td>
                        </tr>""" %("YEAR" ,i["YEAR"])
    if "PLATFORM" in i:
        html_table = html_table + """
                        <tr>
                            <th scope="row">%s</th>
                            <td>%s
                            </td>
                        </tr>""" % ("PLATFORM", i["PLATFORM"])
    if "RELEASED_IN" in i:
        html_table = html_table + """
                        <tr>
                            <th scope="row">%s</th>
                            <td>%s
                            </td>
                        </tr>""" % ("RELEASED_IN", i["RELEASED_IN"])
    if "GENRE" in i:
        GENRE=",".join(i["GENRE"])
        html_table = html_table + """
                        <tr>
                            <th scope="row">%s</th>
                            <td>%s
                            </td>
                        </tr>""" % ("GENRE", GENRE)
    if "Theme" in i:
        Theme = ",".join(i["Theme"])
        html_table = html_table + """
                        <tr>
                            <th scope="row">%s</th>
                            <td>%s
                            </td>
                        </tr>""" % ("Theme", Theme)
    if "PUBLISHER" in i:
        html_table = html_table + """
                        <tr>
                            <th scope="row">%s</th>
                            <td>%s
                            </td>
                        </tr>""" % ("PUBLISHER", i["PUBLISHER"])
    if "DEVELOPER" in i:
        html_table = html_table + """
                        <tr>
                            <th scope="row">%s</th>
                            <td>%s
                            </td>
                        </tr>""" % ("DEVELOPER", i["DEVELOPER"])
    if "DEVELOPER" in i:
        html_table = html_table + """
                        <tr>
                            <th scope="row">%s</th>
                            <td>%s
                            </td>
                        </tr>""" % ("DEVELOPER", i["DEVELOPER"])

    html_table=html_table+"""</tbody>      </table>"""
    htmlForGameRateDL="""</div>
  </div>
  <div class="box gameRateDL">
    <div class="gameRate">
      <div id="grRaB" class="gameRated">
        投票得分：%s
      </div>
    </div>
    <div class="gameDL">
      <a href="%s"
        >Download
              </a>
        </div>
    </div>""" %(i["rate"],i['download'])
    htmlFDescription=""
    if "descripts" in i:
        descripts="".join(i["descripts"])
        descripts=descripts.strip("['").strip("']")
        htmlFDescription="""
        <div class="box">
            <h3>Description of %s</h3>
            %s
            </div>""" %(i["name"],descripts)
    htmlForSnapshots="""    <div class="box">
            <h3>Captures and Snapshots</h3>
            <ul id="screentabs">"""
    if "Screenshot" in i:
        for j in i["Screenshot"]:
            htmlForSnapshots=htmlForSnapshots+"""
                    <div class="items screens" data-platform="4">
                        <img src="%s" alt="%s" loading="lazy" />
                        </picture>
                        </a>
                    </div>""" %(j,i["name"])
    htmlForSnapshots=htmlForSnapshots+""""</ul>
        </div>
    </div>"""
    htmlresutl=html_head+html_table+htmlForGameRateDL+htmlFDescription+htmlForSnapshots
    with open("../../output/detail/%s.html" %i["name"].replace('"',"_").replace("/","_").replace(":","_").replace("?","_").replace("*","_"), mode="wb") as f:
        f.write(htmlresutl.encode("utf-8"))
    print(n,i["name"].replace('"',"_").replace("/","_").replace(":","_").replace("?","_").replace("*","_"))